<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>登录</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Font Awesome -->
  <link rel="stylesheet" href="../../../plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="../../../dist/css/icon.css">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" href="../../../plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../../dist/css/adminlte.min.css">
  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="../../../plugins/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">
  <link href="../../../dist/css/fonts.css" rel="stylesheet">
</head>

<body class="hold-transition login-page">
  <div class="login-box">
    <div class="login-logo">
      <a href="" onclick="return false">计算机维护队</a>
    </div>
    <!-- /.login-logo -->
    <div class="card">
      <div class="card-body login-card-body">
        <form action="index3.html" method="post">
          <div class="input-group mb-3">
            <input type="text" class="form-control" placeholder="学号" id="account" onkeydown=KeyDown()>
            <div class="input-group-append">
              <div class="input-group-text">
                <span class="fas fa-user"></span>
              </div>
            </div>
          </div>
          <div class="input-group mb-3">
            <input type="password" class="form-control" placeholder="密码" id="password" onkeydown=KeyDown()>
            <div class="input-group-append">
              <div class="input-group-text">
                <span class="fas fa-lock"></span>
              </div>
            </div>
          </div>

        </form>


        <div class="social-auth-links text-center mb-3">
          <button class="btn btn-primary btn-block" id="submit_btn">登录</button>
        </div>
        <!-- /.social-auth-links -->
        <a href="/repair/login" class="text-center">游客登录</a>

      </div>
      <!-- /.login-card-body -->
    </div>
  </div>
  <!-- /.login-box -->
  <footer style="position: absolute;top:100%;margin-top: -30px; color:#a59099">
    @CopyRight2020华南农业大学计算机维护队 粤ICP备20026612号
  </footer>
  <!-- jQuery -->
  <script src="../../../plugins/jquery/jquery.min.js"></script>
  <!-- Bootstrap 4 -->
  <script src="../../../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
  <!-- AdminLTE App -->
  <script src="../../../plugins/sweetalert2/sweetalert2.min.js"></script>
  <script src="../../../dist/js/adminlte.min.js"></script>
  <script>
    const Toast = Swal.mixin({
      toast: true,
      showConfirmButton: false,
      timer: 1500
    });
    $("#submit_btn").on("click", function () {
      $.ajax({
        type: 'POST',
        url: '/member/login',
        async: true,
        data: JSON.stringify({
          "account": $("#account").val(),
          "passwordMD5": $("#password").val()
        }),
        dataType: 'text',
        contentType: "application/json",
        success: function (res) {

          if (JSON.parse(res).statusCode === "200") {
            Toast.fire({
              icon: 'success',
              title: ' 登陆成功'
            })
            console.log(JSON.parse(res).role)
            if (JSON.parse(res).role === "队员") {
              setTimeout(function () { window.location.href = "/member/index" }, 1000);
            } else if (JSON.parse(res).role === "管理员") {
              console.log("管理员")
              setTimeout(function () { window.location.href = "/admin/index" }, 1000);
              $.ajax(
                {
                  type: 'POST',
                  url: "/admin/index",
                  async: true,
                  dataType: 'Text',
                  crossDomain: true,
                  xhrFields: {
                    withCredentials: true
                  },
                  data: {
                    "account": $("#account").val(),
                    "role": "管理员"
                  },
                  contentType: "application/x-www-form-urlencoded",
                  success: function (res) {
                    console.log(JSON.parse(res))
                  },
                  error: function (err) {
                    console.log(JSON.stringify(err))
                    console.log(err)
                  }
                })
            } else if (JSON.parse(res).role === "接线员") {
              console.log("接线员")
              setTimeout(function () { window.location.href = "/operator/index" }, 1000);
              $.ajax(
                {
                  type: 'POST',
                  url: "/operator/index",
                  async: true,
                  dataType: 'Text',
                  crossDomain: true,
                  xhrFields: {
                    withCredentials: true
                  },
                  data: {
                    "account": $("#account").val(),
                    "role": "接线员"
                  },
                  contentType: "application/x-www-form-urlencoded",
                  success: function (res) {
                    console.log(JSON.parse(res))
                  },
                  error: function (err) {
                    console.log(JSON.stringify(err))
                    console.log(err)
                  }
                })
            }
          } else {
            Toast.fire({
              icon: 'error',
              title: ' 密码错误'
            })
          }
        },
        error: function (err) {
          console.log("失败" + JSON.stringify(err))
        }
      });
    })
    function KeyDown() {
      if (event.keyCode == 13) {
        event.returnValue = false;
        event.cancel = true;
        $('#submit_btn').click();
      }
    }
  </script>

</body>

</html>